<%@page import="java.util.*,java.text.*" %>
<%@page contentType="text/html;charset=utf-8" %>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>Print a month page.</title>
</head>
<body bgcolor="white">

<%
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=utf-8");
%>

<%  // First get the month and year from the form.
  boolean yyok = false;  // -1 is a valid year, use boolean
  int yy = 0, mm = 0, dd = 1;
  String yyString = request.getParameter("year");
  if (yyString != null && yyString.length() > 0) {
    try {
      yy = Integer.parseInt(yyString);
      yyok = true;
    } catch (NumberFormatException e) {
      out.println("Year " + yyString + " invalid");
    }
  }
  Calendar c = Calendar.getInstance();
  if (!yyok)
    yy = c.get(Calendar.YEAR);

  String mmString = request.getParameter("month");
  if (mmString == null) {
    mm = c.get(Calendar.MONTH);
  } else {
    for (int i=0; i<months.length; i++)
      if (months[i].equals(mmString)) {
        mm = i;
        break;
      }
  }
  String ddString = request.getParameter("day");
  if (ddString == null) {
    dd = 1;
  } else {
    try
    {
        dd = Integer.parseInt(ddString);
    }
    catch (Exception e)
    {
        dd = 1;
    }
  }
  request.setAttribute("day", dd);
 %>

<form method=get action="CalendarPage.jsp">
  Месяц: <select name=month>
  <% for (int i=0; i<months.length; i++) {
    if (i==mm)
      out.print("<option selected>");
    else
      out.print("<option>");
      out.print(months[i]);
      out.println("</option>");
  }
  %>
  <br>
  </select>
  Год (4-цифры):
    <input type="text" size="5" name="year"
      value="<%= yy %>">
    </input>
  <input type=submit value="Показать">
</form>
<%!
  /** The names of the months */
  String[] months = {
    "Январь", "Февраль", "Март", "Апрель",
    "Май", "Июнь", "Июль", "Август",
    "Сентябрь", "Октябрь", "Ноябрь", "Декабрь"
  };
%>

<%
  /** The number of days to leave blank at the start of this month */
  int leadGap = 0;
  /** temporary for links*/
  GregorianCalendar tcalendar = new GregorianCalendar(yy, mm, 1);
  String link_back = "";
  String link_forward = "";
%>
<table border=0>
<tr><th colspan=9><%="<a href=" + '"' + '"' + "<<" + "</a> " + months[mm] %>  <%= yy %>
<a href="<=% + '">></a></tr>

<tr><td>П<td>В<td>С<td>Ч<td>П<td>С<td>В</tr>

<%
    GregorianCalendar calendar = new GregorianCalendar(yy, mm, 1);

    // Compute how much to leave before the first.
    // getDay() returns 0 for Sunday, which is just right.
    leadGap = calendar.get(Calendar.DAY_OF_WEEK)-2;
    if (leadGap < 0) leadGap = 6; 

    int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    if (calendar.isLeapYear(calendar.get(Calendar.YEAR)) && mm == 1)
      ++daysInMonth;

    out.print("<tr>");

    // Blank out the labels before 1st day of month
    for (int i = 0; i < leadGap; i++) {
      out.print("<td>&nbsp;");
    }

    // Fill in numbers for the day of month.
    for (int i = 1; i <= daysInMonth; i++) {

      out.print("<td>");
      out.print("<a href=" + '"' + "/CalendarPage.jsp?&day=" + i + "&month=" + months[mm] + "&year=" + yy + '"' + ">" + i + "</a>");
      out.print("</td>");

      if ((leadGap + i) % 7 == 0) {    // wrap if end of line.
        out.println("</tr>");
        out.print("<tr>");
      }
    }
%>
</tr>
</table>